/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Frm_DsClient.java
 *
 * Created on Nov 30, 2010, 12:49:21 AM
 */

package media_server;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

//import org.w3c.dom.Attr;
//import org.w3c.dom.Comment;
import org.w3c.dom.Document;
//import org.w3c.dom.Element;
//import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author DELL
 */
public class Frm_DsClient extends javax.swing.JFrame {
     public DefaultTableModel tb;
     public static Document document=null;
     Timer t;
     public Document getDocument()
     {
        return document;
     }
    /** Creates new form Frm_DsClient */
    public Frm_DsClient() {
        initComponents();
         this.setTitle("Danh sach Client truy cap den server");
         Dimension smh=Toolkit.getDefaultToolkit().getScreenSize();
        int w=this.getWidth();
        int h=this.getHeight();

        int x=(smh.width-w)/2;
         int y=(smh.height-h)/2;
         this.setLocation(x, y);
         /////////************************************************************/////////////////////////////////
        cmbKenh.setEnabled(false);
        loaddsDangTruyCap();
       
        ///////////////////////////////////////////////


        t = new Timer();
        t.schedule(new TimerTask()
        {

            @Override
            public void run()
            {
                
                   loaddsDangTruyCap();
               
                //System.out.println("aa");
            }

        }, 0,5000);
                        //findElements("InBox.xml","Mails" );

    }

 
    /////////////////////////////////////
    public  void loaddsDangTruyCap()
    {
         try {
            tb =new DefaultTableModel();
            tb.addColumn("No");
            tb.addColumn("NickName");
            tb.addColumn("Ip");
            tb.addColumn("LogIn");
            
            //tb.addColumn("LogIn",Boolean.TYPE.getMethods());
            
            tb.addColumn("Active");
             

           //tbClientDangTC.setModel(tb);
            //loaddsDangNhap(cmbKenh.getSelectedIndex());
            int vt=tab_client.getSelectedIndex();
            loaddsDangNhap(vt);
        } catch (Error ex) {
            Logger.getLogger(Frm_DsClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void loaddsDangNhap(int vt)
    {
        
        if(vt==0)
            tbClientKhongTC.setModel(tb);
        else
            tbClientDangTC.setModel(tb);
         DOMParser parer = new DOMParser();
        try
        {
            parer.parse("Client.xml");
        } catch (SAXException ex) {
            Logger.getLogger(Frm_DsClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex)
        {
            Logger.getLogger(Frm_DsClient.class.getName()).log(Level.SEVERE, null, ex);
        }
            document=(Document)parer.getDocument();
            String n= document.getDocumentElement().getNodeName();
            NodeList node = document.getElementsByTagName("Client");
            int bien=0;
            for(int i=0;i<node.getLength(); i++)
            {
                String S_NickName="";
                String S_Ip="";
                String S_LogIn="";
                String S_Active="";
                S_Ip=node.item(i).getAttributes().item(0).getNodeValue();
                for(int j=0; j<node.item(i).getChildNodes().getLength();j++)
                {

                    if(node.item(i).getChildNodes().item(j).getNodeName().equals("NickName"))
                    {
                        S_NickName=node.item(i).getChildNodes().item(j).getTextContent();
                    }
                     if(node.item(i).getChildNodes().item(j).getNodeName().equals("LogIn"))
                    {
                        S_LogIn=node.item(i).getChildNodes().item(j).getTextContent();
                    }
                      if(node.item(i).getChildNodes().item(j).getNodeName().equals("Active"))
                    {
                        S_Active=node.item(i).getChildNodes().item(j).getTextContent();
                    }
                }
                if(String.valueOf(vt).equals(S_Active))
                {
                    if(S_LogIn.equals("1") && S_Active.equals("1"))
                        tb.insertRow(bien, new Object[]{bien+1,S_NickName,S_Ip,"Yes","Yes"});
                    else
                    {
                        if(S_LogIn.equals("0") && S_Active.equals("1"))
                            tb.insertRow(bien, new Object[]{bien+1,S_NickName,S_Ip,"No","Yes"});
                        else
                        {
                            if(S_LogIn.equals("1") && S_Active.equals("0"))
                                tb.insertRow(bien, new Object[]{bien+1,S_NickName,S_Ip,"Yes","No"});
                            else
                            {
                                if(S_LogIn.equals("0") && S_Active.equals("0"))
                                    tb.insertRow(bien, new Object[]{bien+1,S_NickName,S_Ip,"No","No"});
                            }
                        }
                    }
                    bien=bien+1;
                }
            }
         if(vt!=0)
            tbClientKhongTC.setModel(tb);
         else
             tbClientDangTC.setModel(tb);
//        this.tbClientDangTC.getColumnModel().getColumn(4).setMaxWidth(0);
//        this.tbClientDangTC.getColumnModel().getColumn(4).setMinWidth(0);
//        this.tbClientDangTC.getColumnModel().getColumn(4).setPreferredWidth (0);
    }
    /////////////******************//////////////////////////
    private JTable createTable(JTable tb)
    {
       
        tb.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null,null, null, null, null, null},
                {null,null, null, null, null, null},
                {null,null, null, null, null, null},
                {null,null, null, null, null, null}
            },
            new String [] {
                "No","Check", "NickName", "Ip", "LogIn", "Active"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class,java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        return tb;
    }
////////////////////////////////////////////////////////////

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tab_client = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbClientKhongTC = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbClientDangTC = new javax.swing.JTable();
        cmbKenh = new javax.swing.JComboBox();
        btChoTruyCap = new javax.swing.JButton();
        btKhongChoTruyCap = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cbChonKenh = new javax.swing.JCheckBox();

        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));

        tab_client.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tab_clientStateChanged(evt);
            }
        });

        tbClientKhongTC.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "No", "NickName", "Ip", "Active"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tbClientKhongTC);
        tbClientKhongTC.getColumnModel().getColumn(0).setMinWidth(45);
        tbClientKhongTC.getColumnModel().getColumn(0).setPreferredWidth(45);
        tbClientKhongTC.getColumnModel().getColumn(0).setMaxWidth(45);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
        );

        tab_client.addTab("Client không được phép truy cập", jPanel2);

        tbClientDangTC.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "No", "NickName", "Ip", "LogIn", "Active"
            }
        ));
        jScrollPane2.setViewportView(tbClientDangTC);
        tbClientDangTC.getColumnModel().getColumn(0).setMinWidth(45);
        tbClientDangTC.getColumnModel().getColumn(0).setPreferredWidth(45);
        tbClientDangTC.getColumnModel().getColumn(0).setMaxWidth(45);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
        );

        tab_client.addTab("Client được phép truy cập", jPanel3);

        cmbKenh.setFont(new java.awt.Font("Tahoma", 1, 11));
        cmbKenh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kênh 1", "Kênh 2", "Kênh 3", "Kênh 4" }));
        cmbKenh.setToolTipText("Các kênh trình chiếu");
        cmbKenh.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbKenhItemStateChanged(evt);
            }
        });

        btChoTruyCap.setBackground(new java.awt.Color(0, 0, 255));
        btChoTruyCap.setFont(new java.awt.Font("Tahoma", 1, 11));
        btChoTruyCap.setForeground(new java.awt.Color(255, 255, 255));
        btChoTruyCap.setText("Cho truy cập");
        btChoTruyCap.setToolTipText("Cho phép client truy cập vào server");
        btChoTruyCap.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btChoTruyCapMouseClicked(evt);
            }
        });

        btKhongChoTruyCap.setBackground(new java.awt.Color(0, 0, 255));
        btKhongChoTruyCap.setFont(new java.awt.Font("Tahoma", 1, 11));
        btKhongChoTruyCap.setForeground(new java.awt.Color(255, 255, 255));
        btKhongChoTruyCap.setText("Không cho truy cập");
        btKhongChoTruyCap.setToolTipText("Không cho phép client truy cập vào server");
        btKhongChoTruyCap.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btKhongChoTruyCapMouseClicked(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("DANH SÁCH CLIENT TRUY CẬP");

        cbChonKenh.setBackground(new java.awt.Color(153, 153, 255));
        cbChonKenh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cbChonKenhMouseClicked(evt);
            }
        });
        cbChonKenh.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                cbChonKenhStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbChonKenh)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbKenh, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btChoTruyCap)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btKhongChoTruyCap)
                .addContainerGap(308, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(162, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(132, 132, 132))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tab_client, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(26, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbKenh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btChoTruyCap)
                    .addComponent(btKhongChoTruyCap)
                    .addComponent(cbChonKenh))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tab_client, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbKenhItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbKenhItemStateChanged
        // TODO add your handling code here:
        
    }//GEN-LAST:event_cmbKenhItemStateChanged

    private void cbChonKenhMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cbChonKenhMouseClicked
        // TODO add your handling code here:

        if(cbChonKenh.isSelected()==true)
            cmbKenh.setEnabled(true);
        else
             cmbKenh.setEnabled(false);

    }//GEN-LAST:event_cbChonKenhMouseClicked

    private void tab_clientStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tab_clientStateChanged
        // TODO add your handling code here:
        loaddsDangTruyCap();
        if(tab_client.getSelectedIndex()==0)
        {
            btChoTruyCap.setEnabled(true);
            btKhongChoTruyCap.setEnabled(false);
        }
        else
        {
             btKhongChoTruyCap.setEnabled(true);
            btChoTruyCap.setEnabled(false);
        }
    }//GEN-LAST:event_tab_clientStateChanged

    private void cbChonKenhStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cbChonKenhStateChanged
        // TODO add your handling code here:
       if(cbChonKenh.isSelected()==true)
            cmbKenh.setEnabled(true);
        else
             cmbKenh.setEnabled(false);

    }//GEN-LAST:event_cbChonKenhStateChanged

    private void btChoTruyCapMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btChoTruyCapMouseClicked
        // TODO add your handling code here:
        int vt=tbClientKhongTC.getSelectedRow();
        String S_NickName=tbClientKhongTC.getModel().getValueAt(vt, 1).toString().trim();
        String S_Ip=tbClientKhongTC.getModel().getValueAt(vt, 2).toString().trim();
         String S_LogIn="";
        if(tbClientKhongTC.getModel().getValueAt(vt,3).equals("No"))     
             S_LogIn="0";
        else
        {
            if(tbClientKhongTC.getModel().getValueAt(vt,3).equals("Yes"))
               S_LogIn="1";
        }
        String S_Active="1";

        System.out.println(S_Ip+ " "+S_NickName+ " "+S_LogIn+ " "+S_Active);

        CreateXmlFile_Client.AddNodeDocument_Attri("Client.xml", document,S_NickName,S_Ip, S_LogIn, S_Active);

        tb =new DefaultTableModel();
        tb.addColumn("No");
        tb.addColumn("NickName");
        tb.addColumn("Ip");
        tb.addColumn("LogIn");

        //tb.addColumn("LogIn",Boolean.TYPE.getMethods());

        tb.addColumn("Active");
        loaddsDangNhap(tab_client.getSelectedIndex());
    }//GEN-LAST:event_btChoTruyCapMouseClicked

    private void btKhongChoTruyCapMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btKhongChoTruyCapMouseClicked
        // TODO add your handling code here:
         int vt=tbClientDangTC.getSelectedRow();
        String S_NickName=tbClientDangTC.getModel().getValueAt(vt, 1).toString().trim();
        String S_Ip=tbClientDangTC.getModel().getValueAt(vt, 2).toString().trim();

//        String S_LogIn="";
//        if(tbClientDangTC.getModel().getValueAt(vt,3).equals("No"))
//             S_LogIn="0";
//        else
//        {
//            if(tbClientDangTC.getModel().getValueAt(vt,3).equals("Yes"))
//               S_LogIn="1";
//        }
        String S_Active="0";
        String S_LogIn="0";
        System.out.println(S_Ip+ " "+S_NickName+ " "+S_LogIn+ " "+S_Active);

        CreateXmlFile_Client.AddNodeDocument_Attri("Client.xml", document,S_NickName,S_Ip, S_LogIn, S_Active);
         tb =new DefaultTableModel();
        tb.addColumn("No");
        tb.addColumn("NickName");
        tb.addColumn("Ip");
        tb.addColumn("LogIn");

        //tb.addColumn("LogIn",Boolean.TYPE.getMethods());

        tb.addColumn("Active");
        loaddsDangNhap(tab_client.getSelectedIndex());
    }//GEN-LAST:event_btKhongChoTruyCapMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Frm_DsClient().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btChoTruyCap;
    private javax.swing.JButton btKhongChoTruyCap;
    private javax.swing.JCheckBox cbChonKenh;
    private javax.swing.JComboBox cmbKenh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane tab_client;
    private javax.swing.JTable tbClientDangTC;
    private javax.swing.JTable tbClientKhongTC;
    // End of variables declaration//GEN-END:variables

}
